﻿using System;
using System.Windows.Forms;
using MentalAlchemy.Molecules.Image;

namespace MentalAlchemy.Compounds.Image
{
	public partial class ImageDescriptionParamsControl : UserControl
	{
		#region - Public variables. -
		//public const string[] 
		#endregion

		#region - Public property. -
		/// <summary>
		/// Properties for the image description corresponding to the controls status.
		/// </summary>
		public ImageDescriptionProperties ImageDescriptionProperties
		{
			get
			{
				var props = new ImageDescriptionProperties();
				props.UseLocalBrightness = props.UseLocalColor = LocalColorCheck.Checked;
				props.UseLocalBrightnessVariance = LocalVarianceCheck.Checked;
				props.UseLocalBrightnessEntropy = LocalEntropyCheck.Checked;
				props.UseLocalEdgeOrientation = EdgeCheck.Checked;
				props.UseLocalEdgeOrientation2x2 = CenteredEdgeOrientComboBox.SelectedIndex == 1;
				props.UseLocalEdgeOrientation4x4 = CenteredEdgeOrientComboBox.SelectedIndex == 2;
				props.UseVectorQuantization = UseQuantizationCheck.Checked;
				props.UseVectorAveraging = FinalAverageCheck.Checked;
				props.PointDetectorName = PointDetectorsList.SelectedItem.ToString();
				props.MaxQuantizationVectorsCount = (int)(KohonenNetSize.Value * KohonenNetSize.Value);
				props.MaxImageDimension = ReduceImageCheck.Checked ? (int)MaxImageSizeNumeric.Value : 0;
				props.Radius = (int)RadiusNumeric.Value;

				return props;
			}
		}
		#endregion

		#region - Constructor. -
		public ImageDescriptionParamsControl()
		{
			InitializeComponent();

			foreach (var s in ImageAnalysis.POINT_DETECTORS)
			{
				PointDetectorsList.Items.Add(s);
			}
			PointDetectorsList.SelectedIndex = 1;	// select 'Harris Detector'. Yep, it's a hack.

			CenteredEdgeOrientComboBox.Items.Add("No centered edge description.");
			CenteredEdgeOrientComboBox.Items.Add("2x2 tiling description.");
			CenteredEdgeOrientComboBox.Items.Add("4x4 tiling description.");
			CenteredEdgeOrientComboBox.SelectedIndex = 0;
		} 
		#endregion

		#region - Event handler. -
		private void UseQuantizationCheck_CheckedChanged(object sender, EventArgs e)
		{
			KohonenNetSize.Visible = UseQuantizationCheck.Checked;
			KohonenNetLabel.Visible = UseQuantizationCheck.Checked;
		}

		private void ReduceImageCheck_CheckedChanged(object sender, EventArgs e)
		{
			MaxSizeLabel.Enabled = ReduceImageCheck.Checked;
			MaxImageSizeNumeric.Enabled = ReduceImageCheck.Checked;
		} 
		#endregion

		#region - Public method. -
		/// <summary>
		/// Utility method. Returns suffix corresponding to the current parameters setting for image description.
		/// </summary>
		/// <returns>Suffix for a filename.</returns>
		public string GetFilenameSuffix()
		{
			string res = "";

			if (CenteredEdgeOrientComboBox.SelectedIndex == 0)
			{
				res += "_" + PointDetectorsList.SelectedItem;
				res += ReduceImageCheck.Checked ? "_reduce" + MaxImageSizeNumeric.Value : "";
				res += "_r" + RadiusNumeric.Value;
				res += LocalColorCheck.Checked ? "_color" : "";
				res += LocalEntropyCheck.Checked ? "_ent" : "";
				res += LocalVarianceCheck.Checked ? "_var" : "";
				res += EdgeCheck.Checked ? "_edge" : "";
				res += UseQuantizationCheck.Checked ? "_net" + KohonenNetSize.Value : "";
			}
			else
			{
				//res += ReduceImageCheck.Checked ? "_reduce" + MaxImageSizeNumeric.Value : "";
				res += "_r" + RadiusNumeric.Value;
				res += CenteredEdgeOrientComboBox.SelectedIndex == 2 ? "_edge2x2" : "_edge4x4";
			}
			res += "_" + DateTime.Now.ToShortDateString();

			return res;
		} 
		#endregion
	}
}
